﻿#include "uimodelparametertableview.h"

UiModelParameterTableView::UiModelParameterTableView(QWidget *parent)
    : UiFilterTableView{parent}
{
    connect(m_addBtn, &QPushButton::clicked, this, &UiModelParameterTableView::addParameter);
}

void UiModelParameterTableView::reload(const QString &modelname, const QString &user)
{
    this->clearData();

    if(modelname.isEmpty()) {
        m_modelName.clear();
        m_user.clear();
        return;
    }

    m_user = user;
    m_modelName = modelname;

    QList<QSharedPointer<IModel>> parameters;
    if(!CWebRequestApi::ins().getModelParameterList(parameters, modelname))
        return;

    if(parameters.size() > 0) {
        this->appendData(parameters);
    }
}

void UiModelParameterTableView::addParameter()
{
    if(m_modelName.isEmpty()) {
        ElaMessageBar::error(ElaMessageBarType::Top, "警告", "请先选择操作模型!",2000);
        return;
    }

    QSharedPointer<CRtModelParameterInfo> info(new CRtModelParameterInfo);
    int max_sn = 0;
    QStringList groups;
    QStringList names;
    for(int row = 0; row < m_model->rowCount(); ++row) {
        CRtModelParameterInfo* yxptr = static_cast<CRtModelParameterInfo*>(m_model->getDataByIndex(row).data()) ;
        if(!yxptr)
            continue;

        if(!groups.contains(yxptr->grouping))
            groups.append(yxptr->grouping);

        if(!names.contains(yxptr->name))
            names.append(yxptr->name);

        if(yxptr->sn >= max_sn)
            max_sn = yxptr->sn;
    }

    info->model_name = m_modelName;
    info->user = m_user;
    info->sn = max_sn+1;
    info->name = "";

    QVBoxLayout* mainlayout = new QVBoxLayout;

    UiModelSigBaseEdit editw(info, groups,UiModelSigBaseEdit::ECreate);

    int fontSize = 16;
    int lineHeidht = 32;

    QGroupBox* box = new QGroupBox;
    box->setTitle("参数属性编辑");
    ElaText tmp_type("参数类型:");
    tmp_type.setMinimumHeight(lineHeidht);
    tmp_type.setTextPointSize(fontSize);
    ElaComboBox cbx_type;
    cbx_type.addItems(QStringList() << "整数" << "小数" << "字符串");
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_type,1);
    hlayout->addWidget(&cbx_type,3);
    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addLayout(hlayout);

    ElaText tmp_read("读使能:");
    tmp_read.setMinimumHeight(lineHeidht);
    tmp_read.setTextPointSize(fontSize);
    ElaToggleSwitch btn_read;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_read,1);
    hlayout->addWidget(&btn_read,3);
    vlayout->addLayout(hlayout);

    ElaText tmp_write("写使能:");
    tmp_write.setMinimumHeight(lineHeidht);
    tmp_write.setTextPointSize(fontSize);
    ElaToggleSwitch btn_write;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_write,1);
    hlayout->addWidget(&btn_write,3);
    vlayout->addLayout(hlayout);
    box->setLayout(vlayout);

    QWidget* wtmp = new QWidget;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(box);
    wtmp->setLayout(hlayout);

    mainlayout->addWidget(&editw);
    mainlayout->addWidget(wtmp);

    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->resize(500, 400);
    dialog->setConten(mainlayout);

    while(dialog->exec() == 0) {
        editw.save();

        if(names.contains(info->name)){
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信号名字重复,请重新输入!",2000);
            continue;
        }

        if(info->name.isEmpty() || info->descr.isEmpty() || info->grouping.isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信息不能为空,请检查!",2000);
            continue;
        }

        info->ptype = static_cast<CRtModelParameterInfo::EParameterType>(cbx_type.currentIndex());
        info->en_read = btn_read.getIsToggled();
        info->en_write = btn_write.getIsToggled();

        if(!CWebRequestApi::ins().addParameter(info)) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "添加参数失败",2000);
            break;
        }

        this->appendData(info);
        ElaMessageBar::success(ElaMessageBarType::Top, "完成", "添加参数成功",1500);
        break;
    }
    dialog->deleteLater();
}
